<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="用户昵称" prop="nickName">
      <el-input v-model="form.nickName" maxlength="30" />
    </el-form-item>
    <el-form-item label="手机号码" prop="phonenumber">
      <el-input v-model="form.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="form.email" maxlength="50" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="form.sex">
        <el-radio label="0">男</el-radio>
        <el-radio label="1">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserInfo } from "@/api/aggregation";

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  watch: {
    user: {
      handler(user) {
        if (user) {
          // 后端返回的数据字段映射到前端表单字段
          this.form = {
            nickName: user.nickName || user.name,        // 后端name -> 前端nickName
            phonenumber: user.phonenumber || user.phone, // 后端phone -> 前端phonenumber
            email: user.email,                           // 直接映射
            sex: user.sex || '0'                         // 前端保留sex字段用于显示
          };
        }
      },
      immediate: true
    }
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 使用聚合系统的更新用户信息接口，匹配后端User实体类字段
          const updateData = {
            id: this.user.id,
            name: this.form.nickName,        // 前端nickName -> 后端name
            phone: this.form.phonenumber,    // 前端phonenumber -> 后端phone
            email: this.form.email,          // 前端email -> 后端email (保持不变)
            avatar: this.user.avatar         // 保留现有头像URL
            // 移除sex字段，后端User实体类中没有此字段
          };

          updateUserInfo(updateData).then(response => {
            Logger.log('更新用户信息响应:', response);
            if (response.code === 0) {
              this.$modal.msgSuccess("修改成功");
              // 更新本地用户信息，保持字段名称一致
              this.user.nickName = this.form.nickName;        // 前端显示用nickName
              this.user.phonenumber = this.form.phonenumber;  // 前端显示用phonenumber
              this.user.email = this.form.email;              // 前端显示用email
              // 移除sex字段的本地更新，因为后端不支持
            } else {
              this.$modal.msgError(response.data?.msg || '修改失败');
            }
          }).catch(error => {
            Logger.error('更新用户信息失败:', error);
            this.$modal.msgError('修改失败，请重试');
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
